﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Webbshop.ServiceReference1;
using System.Data;

namespace Webbshop
{
    /// <summary>
    /// Sida för Kassan
    /// Visar upp alla Produkter i Varukorgen
    /// Samt ger möjlighet att genomföra ett Köp
    /// </summary>
    public partial class Checkout : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Hämta varukorg och bind till DataList
                Session["ShoppingCart"] = Functions.GetCart(Session["ShoppingCart"] as Cart);
                dataCart.DataSource = (Session["ShoppingCart"] as Cart).Products;
                dataCart.DataBind();
            }
        }

        protected void dataCart_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "Buy")
            {
                if ((Session["ShoppingCart"] as Cart).Products.Count > 0)
                {
                    Functions.BuyProducts(Session["ShoppingCart"] as Cart);

                    Response.Redirect(Request.Url.AbsolutePath);
                }
            }

            else if (e.CommandName == "Delete")
            {
                Functions.DeleteFromCart(Session["ShoppingCart"] as Cart, e.Item.ItemIndex);
                dataCart.DataBind();

                Response.Redirect(Request.Url.AbsolutePath);
            }
            
        }

        protected void dataCart_ItemCreated(object sender, DataListItemEventArgs e)
        {
            // Ställ in GUI om det finns varor eller inte
            if (e.Item.ItemType == ListItemType.Footer) // Hämta knapp i footer-template
            {
                if (dataCart.Items.Count == 0)
                {
                    // Kontrollera vilket språk
                    if (Request.Cookies["Culture"].Value == "sv-SE")
                        lblNoProducts.Text = "Du har inte lagt till något i Varukorgen. Gå och handla!";
                    else
                        lblNoProducts.Text = "You havn't put anything in your ShoppingCart. Please go shop!";
                    
                    e.Item.Enabled = false;
                }
                else
                {
                    lblNoProducts.Text = "";
                    e.Item.Enabled = true;
                }
            }
        }
    }
}